package servlet.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servlet.bean.TeacherBean;
import servlet.model.ServiceFactory;
import servlet.publicUtils.MyUtils;

public class TeaBatchAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//request.setCharacterEncoding("UTF-8");
		TeacherBean teaB = (TeacherBean) MyUtils.getNewInstance(TeacherBean.class);
		
		teaB.setStu_lenth(MyUtils.obj2Integer(request.getParameter("stulenth")));
		
		//取到stu_lenth的值，当做for循环的长度
		for(int j=1;j<=teaB.getStu_lenth();j++){
			String str="sturecord"+j;
			String str1="stunum"+j;
		teaB.setStu_num(MyUtils.obj2Integer(request.getParameter(str1)));
		teaB.setStu_record(MyUtils.obj2Integer(request.getParameter(str)));
		
		//新建用户
		int i=ServiceFactory.getUserInfoManagerServiceInstance().saveStuBatch(teaB);
		
		if(i>0){
			List<TeacherBean> lstStu=ServiceFactory.getUserInfoManagerServiceInstance().getAllStuInfo();
			request.getSession().setAttribute("lstStu", lstStu);
			request.removeAttribute("teaB");
			
			
		}else{
			request.getSession().setAttribute("pageErrorInfo", "用户信息添加失败");
			response.sendRedirect("logined/teaerror.jsp");
		}
		
		}
		request.getRequestDispatcher("logined/teaBatchAddDetail.jsp").forward(request, response);
		
	}

}
